package com.dji.compose_learning.ui.view

import android.os.Parcelable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.Button
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.mapSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlinx.parcelize.Parcelize

@Parcelize
data class City(var country: String, var city: String) : Parcelable

data class UserInfo(var userName: String, var password: String)

data class SchoolInfo(var schoolName: String, var address: String, var level:String)

@Composable
fun RememberStateView() {
    Surface(modifier = Modifier
        .fillMaxSize()
        .padding(16.dp)) {

        Column {
            //sample 1
            var inputText by remember {
                mutableStateOf("")
            }
            var inputText1 by rememberSaveable {
                mutableStateOf("")
            }
            Column {
                Row {
                    BasicTextField(value = inputText, onValueChange = {
                        inputText = it
                    })
                    Button(onClick = { /*TODO*/ }, enabled = inputText.isNotBlank()) {
                        Text(text = "Add")
                    }
                }
                Spacer(modifier = Modifier.height(20.dp))
                Row {
                    BasicTextField(value = inputText1, onValueChange = {
                        inputText1 = it
                    })
                    Button(onClick = { /*TODO*/ }, enabled = inputText1.isNotBlank()) {
                        Text(text = "Add")
                    }
                }
            }

            //sample 2
            var city by remember {
                mutableStateOf(City("China", "BeiJing"))
            }
            var city1 by rememberSaveable {
                mutableStateOf(City("England", "Landon")) //这里需要存储可序列化对象
            }
            Spacer(modifier = Modifier.height(20.dp))
            Column {
                Text(text = "${city.country}-${city.city}")
                Text(text = "${city1.country}-${city1.city}")
                Button(onClick = {
                    //无效设置
//                    city.city = "NanTong"
//                    city1.city = "NanTong"
                    city = City("China", "NanTong")
                    city1 = City("China", "NanTong")
                }) {
                    Text(text = "Change")
                }
            }

            //sample 3 自定义saver
            val userSaver = kotlin.run {
                val nameKey = "Name"
                val passwordKey = "Password"

                mapSaver(
                    save = { mapOf(nameKey to it.userName, passwordKey to it.password) },
                    restore = { UserInfo(it[nameKey] as String, it[passwordKey] as String) }
                )
            }

            var userInfo by rememberSaveable(stateSaver = userSaver) {
                mutableStateOf(UserInfo("buobao", "password"))
            }
            Spacer(modifier = Modifier.height(20.dp))
            Column {
                Text(text = "${userInfo.userName}-${userInfo.password}")
                Button(onClick = {
                    userInfo = UserInfo("buobao", "123456")
                }) {
                    Text(text = "Change Password")
                }
            }

            //sample 4 通过list存储
            val schoolSaver = listSaver<SchoolInfo, String>(save = {
                listOf(it.schoolName, it.address, it.level)
            }, restore = {
                SchoolInfo(it[0], it[1], it[2])
            })
            var schoolInfo by rememberSaveable(stateSaver = schoolSaver) {
                mutableStateOf(SchoolInfo("NanTong Higher School", "Study Road.NT", "5"))
            }
            Spacer(modifier = Modifier.height(20.dp))
            Column {
                Text(text = "${schoolInfo.schoolName},${schoolInfo.address},${schoolInfo.level}")
                Button(onClick = {
                    schoolInfo = SchoolInfo("SuZhou Higher School", "Xiang Cheng Road.SZ", "4")
                }) {
                    Text(text = "Change School")
                }
            }
        }
    }
}


















